{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #招式拆解 II"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #queue #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #队列 #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: dismantlingAction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #招式拆解 II"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>某套连招动作记作仅由小写字母组成的序列 <code>arr</code>，其中 <code>arr[i]</code> 第 <code>i</code> 个招式的名字。请返回第一个只出现一次的招式名称，如不存在请返回空格。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>arr = \"abbccdeff\"\n",
    "<strong>输出：</strong>'a'\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>arr = \"ccdd\"\n",
    "<strong>输出：</strong>' '\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>限制：</strong></p>\n",
    "\n",
    "<p><code>0 &lt;= arr.length&nbsp;&lt;= 50000</code></p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof](https://leetcode.cn/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof](https://leetcode.cn/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"abbccdeff\"', '\"ccdd\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hmap = {}\n",
    "        for c in arr:\n",
    "            hmap[c] = not c in hmap\n",
    "        for c in arr:\n",
    "            if hmap[c]: return c\n",
    "        return ' '\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, s: str) -> str:\n",
    "        dic = {}\n",
    "        for c in s:\n",
    "            dic[c] = not c in dic\n",
    "        for c in s:\n",
    "            if dic[c]: return c\n",
    "        return ' '\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        return next((i for i in arr if arr.count(i)==1),\" \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        dic = dict() # python3中字典默认是有序的\n",
    "        for s in arr:\n",
    "            dic[s] = not s in dic\n",
    "        for k, v in dic.items():\n",
    "            if v is True: return k\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, list: str) -> str:\n",
    "        res = \" \"\n",
    "        # 暴力解法\n",
    "        word_dict = defaultdict(int)\n",
    "        for l in list:\n",
    "            word_dict[l] += 1\n",
    "        for l in list:\n",
    "            if word_dict[l] == 1:\n",
    "                return l\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        heatmap = {}\n",
    "        for i in range(len(arr)):\n",
    "            if arr[i] in heatmap:\n",
    "                heatmap[arr[i]] = False\n",
    "            else:\n",
    "                heatmap[arr[i]] = True\n",
    "        for key,value in enumerate(heatmap):\n",
    "            if heatmap[value] == True:\n",
    "                return value\n",
    "        return ' '\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        count = Counter(arr)\n",
    "        for k,v in count.items():\n",
    "            if v==1:\n",
    "                return k\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        new_dict = {}\n",
    "        if len(arr) == 0:\n",
    "            return \" \"\n",
    "        for element in arr:\n",
    "            if element not in new_dict:\n",
    "                new_dict[element] = 1\n",
    "            else:\n",
    "                new_dict[element] += 1\n",
    "        for key in new_dict.keys():\n",
    "            if new_dict[key] == 1:\n",
    "                return key\n",
    "        return \" \"       \n",
    "        \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        c = collections.Counter(arr)\n",
    "\n",
    "        for ch in arr:\n",
    "            if c[ch] == 1:\n",
    "                return ch \n",
    "        return \" \""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, list: str) -> str:\n",
    "        dic=collections.OrderedDict()\n",
    "        for c in list:\n",
    "            dic[c]=not c in dic\n",
    "        for k,v in dic.items():\n",
    "            if v:return k\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        record_1=[]\n",
    "        record_2=[]\n",
    "        for i in arr:\n",
    "            if i in record_2:\n",
    "                pass\n",
    "            elif i in record_1:\n",
    "                record_1.remove(i)\n",
    "                record_2.append(i)\n",
    "            else:\n",
    "                record_1.append(i)\n",
    "        if len(record_1)!=0:\n",
    "            return record_1[0]\n",
    "        else:\n",
    "            return \" \""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        dic={}\n",
    "        for i in arr:\n",
    "            if i in dic:\n",
    "                dic[i]+=1\n",
    "            else:\n",
    "                dic[i]=0\n",
    "        for j in dic:\n",
    "            if dic[j]==0:\n",
    "                return j\n",
    "        return \" \"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        ans = ' '\n",
    "        hash = dict()\n",
    "        for c in arr:\n",
    "            hash[c] = hash.get(c, 0) + 1\n",
    "        for key, value in hash.items():\n",
    "            if value == 1:\n",
    "                ans = key\n",
    "                break\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hmap = {}\n",
    "        for c in arr:\n",
    "            hmap[c] = not c in hmap\n",
    "        for c in arr:\n",
    "            if hmap[c]:\n",
    "                return c \n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        cnt = Counter(arr)\n",
    "        for ele in arr:\n",
    "            if cnt[ele] == 1:\n",
    "                return ele\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hmap = {}\n",
    "        for c in arr:\n",
    "            hmap[c] = not c in hmap\n",
    "        for c in arr:\n",
    "            if hmap[c]: return c\n",
    "        return ' '\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        dic={}\n",
    "        for i in arr:\n",
    "            if i in dic:\n",
    "                dic[i]+=1\n",
    "            else:\n",
    "                dic[i]=0\n",
    "        for j in dic:\n",
    "            if dic[j]==0:\n",
    "                return j\n",
    "        return \" \"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        ans = \" \"\n",
    "        i = 0\n",
    "        while i < len(arr):\n",
    "            if i != 0 and arr[i]  in arr[0:i]:\n",
    "                i+=1\n",
    "                continue\n",
    "            j = len(arr) - 1\n",
    "            while j > i and arr[i] != arr[j]:\n",
    "                # print(i,j)\n",
    "                j -= 1\n",
    "            if j == i: return arr[i]\n",
    "            i += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        i = 0\n",
    "        hashmap = {}\n",
    "        sortlist = []\n",
    "        for char in arr:\n",
    "            if char in hashmap:\n",
    "                hashmap[char] += 1\n",
    "            else:\n",
    "                hashmap[char] = 1\n",
    "        for char in arr:\n",
    "            if hashmap[char] == 1:\n",
    "                return char\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        dic={}\n",
    "        for string in arr:\n",
    "            dic[string]=dic.get(string,0)+1\n",
    "\n",
    "        for string in dic:\n",
    "            if dic[string]==1:\n",
    "                return string\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        if len(arr) == 0: return ' '\n",
    "        hash_dict = {}\n",
    "        for s in arr:\n",
    "            hash_dict[s] = (s not in hash_dict.keys())\n",
    "        for k, v in hash_dict.items():\n",
    "            if v:\n",
    "                return k\n",
    "        return ' '\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        dic={}\n",
    "        for i in arr:\n",
    "            if i in dic:\n",
    "                dic[i]+=1\n",
    "            else:\n",
    "                dic[i]=0\n",
    "        for j in dic:\n",
    "            if dic[j]==0:\n",
    "                return j\n",
    "        return \" \"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        for i in range(len(arr)):\n",
    "            if arr.count(arr[i]) == 1:\n",
    "                return arr[i]\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        dic = {}\n",
    "        for c in arr:\n",
    "            dic[c] = not c in dic\n",
    "        for c in arr:\n",
    "            if dic[c]:\n",
    "                return c\n",
    "        return \" \""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        map = {}\n",
    "        for ch in arr:\n",
    "            if ch not in map:\n",
    "                map[ch] = 1\n",
    "            else:\n",
    "                map[ch] += 1\n",
    "        for k, v in map.items():\n",
    "            if v == 1:\n",
    "                return k\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        dic={}\n",
    "        for i in arr:\n",
    "            # if i not in dic:\n",
    "            #     dic[i]=0\n",
    "            # else:\n",
    "            #     dic[i]+=1\n",
    "            dic[i]=not i in dic\n",
    "        for u in arr:\n",
    "            if dic[u]:\n",
    "                return u\n",
    "            # else:\n",
    "            #     return ' '\n",
    "        return ' '\n",
    "\n",
    "\n",
    "# class Solution:\n",
    "#     def dismantlingAction(self, arr: str) -> str:\n",
    "#         hmap = {}\n",
    "#         for c in arr:\n",
    "#             hmap[c] = not c in hmap\n",
    "#         for c in arr:\n",
    "#             if hmap[c]: return c\n",
    "#         return ' '\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        if not arr: return ' '\n",
    "\n",
    "        memory = {}\n",
    "        answer = {}\n",
    "\n",
    "        for i in range(len(arr)):\n",
    "            if arr[i] in memory:\n",
    "                if arr[i] in answer:\n",
    "                    del answer[arr[i]]\n",
    "            else:\n",
    "                memory[arr[i]] = None\n",
    "                answer[arr[i]] = None\n",
    "\n",
    "        if len(answer) == 0:\n",
    "            return ' '\n",
    "        else:\n",
    "            return list(answer)[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        cnt = [0 for _ in range(256)]\n",
    "        for c in arr:\n",
    "            cnt[ord(c)] += 1\n",
    "        for c in arr:\n",
    "            if cnt[ord(c)] == 1:\n",
    "                return c\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hmap={}\n",
    "        for c in arr:\n",
    "            hmap[c]=c not in hmap\n",
    "        for k,v in hmap.items():\n",
    "            if(v):\n",
    "                return k\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hmap = collections.OrderedDict()\n",
    "        for c in arr:\n",
    "            hmap[c] = not c in hmap\n",
    "        for k, v in hmap.items():\n",
    "            if v: return k\n",
    "        return ' '\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        set_arr = collections.Counter(arr)\n",
    "        for i,v in set_arr.items():\n",
    "            if v == 1:\n",
    "                return i\n",
    "        \n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        \"\"\"\n",
    "        i = 0\n",
    "        dic = {}\n",
    "        \n",
    "        for k in range(len(arr)):\n",
    "            if arr[k] in dic:\n",
    "                dic[arr[k]][1] += 1\n",
    "            else:\n",
    "                dic[arr[k]] = [i, 1]\n",
    "                i += 1\n",
    "        \n",
    "        # print(dic)\n",
    "        for key, v in dic.items():\n",
    "            if v[1] == 1:\n",
    "                return key\n",
    "      \n",
    "        return \" \"\n",
    "        \"\"\"\n",
    "\n",
    "        # 更简洁：\n",
    "        hmap = {}\n",
    "        for c in arr:\n",
    "            # 用1代表只出现1次，0代表出现过多次：\n",
    "            hmap[c] = c not in hmap\n",
    "        for c in arr:\n",
    "            if hmap[c]: return c\n",
    "        return \" \"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hmap = {}\n",
    "        for c in arr:\n",
    "            hmap[c] = not c in hmap\n",
    "        for c in arr:\n",
    "            if hmap[c]: return c\n",
    "        return ' '\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hmap={}\n",
    "        for i in arr:\n",
    "            hmap[i]= i not in hmap\n",
    "        for i in hmap:\n",
    "            if hmap[i]:\n",
    "                return i\n",
    "        return ' '\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hashmap = {}\n",
    "        for a in arr:\n",
    "            hashmap[a] = not a in hashmap\n",
    "        for i,j in hashmap.items():\n",
    "            if j : return i\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        from collections import Counter\n",
    "        if not arr: return ' '\n",
    "        counter = dict(Counter(arr))\n",
    "        for i in counter:\n",
    "            if counter[i] == 1:\n",
    "                return i\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        dict = {}\n",
    "        for c in arr:\n",
    "            dict[c] = not c in dict\n",
    "        for c in arr:\n",
    "            if dict[c]:\n",
    "                return c\n",
    "        return ' '\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hmap = {}\n",
    "        for c in arr:\n",
    "            hmap[c] = not c in hmap\n",
    "        for k, v in hmap.items():\n",
    "            if v: return k\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        for i in arr:\n",
    "            if arr.count(i) == 1:\n",
    "                return i\n",
    "        return \" \""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        ans = {}\n",
    "        for c in arr:\n",
    "            if c in ans:\n",
    "                ans[c] = False\n",
    "            else:\n",
    "                ans[c] = True\n",
    "        for c in arr:\n",
    "            if ans[c]:\n",
    "                return c\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        dic = {}\n",
    "        for i in arr:\n",
    "            if i not in dic: dic[i] = 1\n",
    "            else: dic[i] += 1\n",
    "        for k, v in dic.items():\n",
    "            if v == 1:\n",
    "                return k\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        # if len(arr) == 0:\n",
    "        #     return ' '\n",
    "        # d = {}\n",
    "        # for i in range(len(arr)):\n",
    "        #     if arr[i] in d:\n",
    "        #         d[arr[i]] = -1\n",
    "        #     else:\n",
    "        #         d[arr[i]] = i\n",
    "        # ans = None\n",
    "        # idx = len(arr)\n",
    "        # for k, v in d.items():\n",
    "        #     if v != -1 and v < idx:\n",
    "        #         ans, idx = k, v\n",
    "        # return ans if ans else ' '\n",
    "        \n",
    "        # # Collections解法\n",
    "        # try:\n",
    "        #     return arr[min([arr.index(c) for c, v in collections.Counter(arr).items() if v == 1])]\n",
    "        # except ValueError:\n",
    "        #     return ' '\n",
    "        \n",
    "        hmap = collections.OrderedDict()\n",
    "        for c in arr:\n",
    "            hmap[c] = not c in hmap\n",
    "        for k, v in hmap.items():\n",
    "            if v: return k\n",
    "        return ' '\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hashmap = {}\n",
    "        for i in arr:\n",
    "            if i in hashmap:\n",
    "                hashmap[i] += 1\n",
    "            else:\n",
    "                hashmap[i] = 1\n",
    "        for i in hashmap.keys():\n",
    "            if hashmap[i] == 1:\n",
    "                return i \n",
    "        return ' '\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        from collections import Counter\n",
    "        if not arr: return ' '\n",
    "        counter = dict(Counter(arr))\n",
    "        for i in counter:\n",
    "            if counter[i] == 1:\n",
    "                return i\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hmap = {}\n",
    "        for i in arr:\n",
    "            hmap[i] =  not i in hmap\n",
    "        for i in arr:\n",
    "            if hmap[i]:\n",
    "                return i\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        for i in arr:\n",
    "            if arr.count(i)==1:\n",
    "                return i\n",
    "            \n",
    "        return \" \"\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        dict1 = collections.defaultdict(int)\n",
    "        for s in arr:\n",
    "            dict1[s] += 1\n",
    "        for key, val in dict1.items():\n",
    "            if val == 1:\n",
    "                return key\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        dic = {}\n",
    "        for c in arr:\n",
    "            dic[c] = not c in dic\n",
    "        for c in arr:\n",
    "            if dic[c]:\n",
    "                return c\n",
    "        return \" \""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        count = [0 for _ in range(26)]\n",
    "        for c in arr:\n",
    "            count[ord(c) - ord('a')] += 1\n",
    "        for c in arr:\n",
    "            if count[ord(c) - ord('a')] == 1:\n",
    "                return c\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        # if len(arr) == 0:\n",
    "        #     return ' '\n",
    "        # d = {}\n",
    "        # for i in range(len(arr)):\n",
    "        #     if arr[i] in d:\n",
    "        #         d[arr[i]] = -1\n",
    "        #     else:\n",
    "        #         d[arr[i]] = i\n",
    "        # ans = None\n",
    "        # idx = len(arr)\n",
    "        # for k, v in d.items():\n",
    "        #     if v != -1 and v < idx:\n",
    "        #         ans, idx = k, v\n",
    "        # return ans if ans else ' '\n",
    "        \n",
    "        # # Collections解法\n",
    "        try:\n",
    "            return arr[min([arr.index(c) for c, v in collections.Counter(arr).items() if v == 1])]\n",
    "        except ValueError:\n",
    "            return ' '\n",
    "        \n",
    "        # hmap = collections.OrderedDict()\n",
    "        # for c in arr:\n",
    "        #     hmap[c] = not c in hmap\n",
    "        # for k, v in hmap.items():\n",
    "        #     if v: return k\n",
    "        # return ' '\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hasmap = {}\n",
    "\n",
    "        for item in arr:\n",
    "            if item not in hasmap:\n",
    "                hasmap[item] = 1\n",
    "            else:\n",
    "                hasmap[item] = hasmap.get(item) + 1\n",
    "        \n",
    "        for key, value in hasmap.items():\n",
    "            if value == 1:\n",
    "                return key\n",
    "        return \" \""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        dic = {}\n",
    "        for c in arr:\n",
    "            dic[c] = not c in dic\n",
    "        for k,v in dic.items():\n",
    "            if v:\n",
    "                return k\n",
    "        return \" \""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        d = {}\n",
    "        for char in arr:\n",
    "            if char in d.keys():\n",
    "                d[char] += 1\n",
    "            else:\n",
    "                d[char] = 1\n",
    "        for k in d.keys():\n",
    "            if d[k] == 1:\n",
    "                return k\n",
    "        return \" \""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hmap = {}\n",
    "        for c in arr:\n",
    "            hmap[c] = not c in hmap\n",
    "        for c in arr:\n",
    "            if hmap[c]: return c\n",
    "        return ' '\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hash_t = dict()\n",
    "        res = ' '\n",
    "        l = len(arr)\n",
    "        for i in range(l):\n",
    "            if not arr[i] in hash_t:\n",
    "                hash_t.update({arr[i]:1})\n",
    "            else:\n",
    "                hash_t[arr[i]] += 1\n",
    "        for i,val in enumerate(hash_t):\n",
    "            if hash_t[val] == 1:\n",
    "                return val\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hamp = {}\n",
    "        for i in arr:\n",
    "            if i not in hamp:\n",
    "                hamp[i] = True\n",
    "            else:\n",
    "                hamp[i] = False\n",
    "        for j in arr:\n",
    "            if hamp[j] == True:\n",
    "                return j\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        cnt = Counter(arr)\n",
    "        for k in cnt:\n",
    "            if cnt[k] == 1:\n",
    "                return k\n",
    "        return \" \""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hmap={}\n",
    "        for c in arr:\n",
    "            hmap[c]=c not in hmap\n",
    "        for c in arr:\n",
    "            if(hmap[c]):\n",
    "                return c\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        list = []\n",
    "        for i in arr:\n",
    "            if i not in list:\n",
    "                list.append(i)\n",
    "        for i in list:\n",
    "            if arr.count(i)==1:\n",
    "                return i\n",
    "        return ' '\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        for i in arr:\n",
    "            if arr.count(i)==1:\n",
    "                return i \n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        dic = {}\n",
    "        for i in arr:\n",
    "            if i not in dic: dic[i] = 1\n",
    "            else: dic[i] += 1\n",
    "        for i in arr:\n",
    "            if dic[i] == 1:\n",
    "                return i\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        dic = {}\n",
    "        for i in arr:\n",
    "            if i not in dic: dic[i] = 1\n",
    "            else: dic[i] += 1\n",
    "        for i in arr:\n",
    "            if dic[i] == 1:\n",
    "                return i\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        if len(arr) == 0:\n",
    "            return \" \"\n",
    "        elif len(arr) == 1:\n",
    "            return arr\n",
    "        else:\n",
    "            while len(arr) > 1:\n",
    "                for j in arr[1:]:\n",
    "                    if j == arr[0]:\n",
    "                        arr = arr.replace(j, '')\n",
    "                        break\n",
    "                else:\n",
    "                    return arr[0]\n",
    "            if len(arr) == 1:\n",
    "                return arr\n",
    "            else:\n",
    "                return \" \"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        # for i in arr:\n",
    "        #     if arr.count(i)==1:\n",
    "        #         return i\n",
    "            \n",
    "        # return \" \"\n",
    "        s = {}\n",
    "        for i in arr:\n",
    "            s[i] = not i in s\n",
    "        for k,v in s.items():\n",
    "            if v:\n",
    "                return k\n",
    "        return \" \""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        lst=[]\n",
    "        for i in arr:\n",
    "            if i not in lst:\n",
    "                cnt=arr.count(i)\n",
    "                if cnt==1:\n",
    "                    return i\n",
    "                    break\n",
    "        else:\n",
    "            return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        hmap=[0]*26\n",
    "        a=ord('a')\n",
    "        for c in arr:\n",
    "            hmap[ord(c)-a]+=1\n",
    "        for c in arr:\n",
    "            if hmap[ord(c)-a]==1:\n",
    "                return c\n",
    "        return ' '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dismantlingAction(self, arr: str) -> str:\n",
    "        q1, q2 = [], []\n",
    "        for i in arr:\n",
    "            if i in q2 and i in q1:\n",
    "                q1.remove(i)\n",
    "            if i not in q2 and i not in q1:\n",
    "                q1.append(i)\n",
    "                q2.append(i)\n",
    "            \n",
    "        if q1:\n",
    "            return q1[0]\n",
    "        else:\n",
    "            return \" \"\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
